********************************************************************************
** Do file: g7_baseline_analysis.do
** First started: June 11, 2020 
** Last edited: September 8, 2023

/* Purpose: this do file compares baseline survey variables between groups
Part I: Set Up
Part II: Statistics for Baseline Characteristics table

* ITEM MADE: TABLE A.7
*/

********************************************************************************

clear
clear matrix
clear mata
set maxvar 10000

************
** Part I **
************

* Load the key dataset
use "$merged/key_rep.dta", clear

* Merge baseline data onto the above
merge m:1 hhid using "$bsvy_clean/hhvars_baseline.dta"
assert _merge!=2
drop _merge

* Statistic for income
sum hhincome_other if k_complete_may==1

* Develop filter for sampling framework, going all the way to endline
g tag = 1 if k_complete_may==1 & k_rolling_list==1 & k_interacted_success==1 & k_surveyed==1 & k_tookloan_repay==1

* Develop a variable that tags those that are in a main treatment group of interest
g maintreat = 1 if treatmenttype_sh=="R T1-L" | treatmenttype_sh == "R T1-U" | treatmenttype_sh == "R T2-U"

* Develop treatment variables
g locked = (treatmenttype_sh=="R T1-L")
g su = (treatmenttype_sh=="R T1-U")
g unlocked = (treatmenttype_sh=="R T2-U")
g control = (treatmenttype_sh=="R C")

* Drop choice treatment
drop if treatmenttype_sh=="R T3"

* Divide the raw income numbers by 3704 to make USD
foreach var of varlist light_spend_year hhincome_other value_hh_assets total_loans {
	replace `var' = `var'/3704
}

*************
** Part II **
*************

* Generate copies for imputation using the mean
foreach var of varlist accountpercentlocked_may headage headsex headmarried headworks_fam headworks_self headworks_out num_hh n_hhenrolled light_spend_year hhincome_other value_hh_assets any_loans total_loans ever_refused microfloan {
	g II`var' = `var'
}
* Generate imputated values and dummies for missing
foreach var of varlist II* {
	g `var'_dum = (`var'==.)
	egen `var'_mean = mean(`var') if tag==1
	replace `var'=`var'_mean if `var'==.
	drop `var'_mean
}

* Keep to either Secured or Unsecured
keep if treatmenttype_sh=="R T1-L" | treatmenttype_sh=="R T2-U"

***** TABLE A.7 *****

ttest IIaccountpercentlocked_may if tag==1, by(treatmenttype_sh)
ttest IIheadage if tag==1, by(treatmenttype_sh)
ttest IIheadsex if tag==1, by(treatmenttype_sh)
ttest IIheadmarried if tag==1, by(treatmenttype_sh)
ttest IIheadworks_fam if tag==1, by(treatmenttype_sh)
ttest IIheadworks_self if tag==1, by(treatmenttype_sh)
ttest IIheadworks_out if tag==1, by(treatmenttype_sh)
ttest IInum_hh if tag==1, by(treatmenttype_sh)
ttest IIn_hhenrolled if tag==1, by(treatmenttype_sh)
ttest IIlight_spend_year if tag==1, by(treatmenttype_sh)
ttest IIhhincome_other if tag==1, by(treatmenttype_sh)
ttest IIvalue_hh_assets if tag==1, by(treatmenttype_sh)
ttest IIany_loans if tag==1, by(treatmenttype_sh)
ttest IItotal_loans if tag==1, by(treatmenttype_sh)
ttest IIever_refused if tag==1, by(treatmenttype_sh)
ttest IImicrofloan if tag==1, by(treatmenttype_sh) 

* Joint test
g lockedreg = (treatmenttype_sh=="R T1-L")
reg lockedreg IIaccountpercentlocked_may IIheadage IIheadsex IIheadmarried IIheadworks_fam IIheadworks_self  IIheadworks_out IInum_hh IIn_hhenrolled IIlight_spend_year IIhhincome_other IIvalue_hh_assets IIany_loans IItotal_loans IIever_refused IImicrofloan if tag==1 // p = 0.0674 from F-test
reg lockedreg IIaccountpercentlocked_may IIheadage IIheadsex IIheadmarried IIheadworks_fam IIheadworks_self  IIheadworks_out IInum_hh IIn_hhenrolled IIlight_spend_year IIhhincome_other IIvalue_hh_assets IIany_loans IItotal_loans IIever_refused IImicrofloan
